<?php

class process_shipping extends a_base_controller_l_checkout {

    protected $sm;
    protected $carriers;
    
    public function __construct() {
        parent::__construct();
        $this->sm = new shipping_manager();

        //Accessing this page will always reset your previous shipping information
        $this->__reset_shipping();

        //load carrier data        
        $this->carriers = $this->sm->get_carriers_data("1", "cad"); //@TO_DO currency/language come from a core function
        if (count($this->carriers) <= 0)
            show_error("No shipping_carriers found!!");

        //load carrier rates objects
        $tmp = $this->session->userdata(module_u_checkout_shipping::SESS_SHIPPING_CARRIERS, TRUE);
        $tmp = $this->__get_carriers_with_rates();
        if (!is_array($tmp)) {
            $tmp = $this->__get_carriers_with_rates();
            $this->session->set_userdata(module_u_checkout_shipping::SESS_SHIPPING_CARRIERS, serialize($tmp));
        }
        $this->carriers = $tmp;

        //smarty assigns
        $this->assign("carriers", $this->carriers);
        $this->assign("shipiping_carrier_selection", $this->input->post("carrier") ? $this->input->post("carrier") : "");
    }

    public function  next() {
        event_dispatcher::i()->fire(module_u_checkout_shipping::EVENT_CHECKOUT_SHIPPING_EXECUTED,array('order_obj'=>$this->order_object));
        parent::next();
    }
    
    protected function __reset_shipping() {
        if ($this->get_order_object()->shipping_carrier_id !== NULL && strlen($this->get_order_object()->shipping_carrier_id)) {
            $carrier_obj = $this->sm->get_carrier_object($this->get_order_object()->shipping_carrier_id);
            $carrier_obj->set_order_object($this->get_order_object());
            $carrier_obj->clear_shipment_info();
        }
        $this->get_order_object()->reset_shipping();
    }

    protected function __get_carriers_with_rates() {

        $multi_curls = new multiple_curl_threads();
        $site = service_l_core::get_current_site();

        foreach ($this->carriers as $carrier) {
            $multi_curls->add_post_data($carrier->carrier_id, "carrier_id", $carrier->carrier_id);
            $multi_curls->add_post_data($carrier->carrier_id, "order_id", $this->order_object->id);
            $multi_curls->add_handler(
                    $carrier->carrier_id,
                    $site->url . $this->config->item('index_page') . "/checkout_shipping/run_carrier/"
            );
        }

        $multi_curls->start();

        foreach ($this->carriers as $carrier) {
            $response = $multi_curls->get_response($carrier->carrier_id);
            if (!$response[multiple_curl_threads::ERROR]) {
                $this->carriers[$carrier->carrier_id]->rates = @unserialize($response[multiple_curl_threads::CONTENT]);
            } else {
                $this->carriers[$carrier->carrier_id]->rates = NULL;
            }
        }
        return $this->carriers;
    }

    protected function __handle_post(){
        $areturn = FALSE;
        $carrier = $this->input->post('carrier');
        if ($carrier) {
            $carrier_obj = $this->sm->get_carrier_object($carrier);
            if($carrier_obj)
            {
                $carrier_obj->set_order_object($this->order_object);
                $return = $carrier_obj->save_shipping_info($this->carriers[$carrier]->rates, $this->input);
                if(!$return){
                    $this->add_checkout_msg($carrier_obj->get_error_message());
                }
            }
        }
        return $return;
    }
    public function __handle_executed_before($is_post_request) {
        if ($is_post_request) {
            if($this->__handle_post()){
                $this->stage_executed();
                $this->next();
            }else{
                $this->view();
            }
        } else {
            $this->view();
        }
    }

    public function __handle_executed_never($is_post_request) {
        if ($is_post_request) {
            if($this->__handle_post()){
                $this->stage_executed();                
                $this->next();
            }else{
                $this->view();
            }
        } else {
            $this->view();
        }
    }

    public function __handle_invalid_stage(stage_l_checkout $valid_stage) {
        redirect($valid_stage->get_action_url());
    }

    public function stage_id() {
        return "shipping";
    }

}

?>